﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public class CtrlFunctionalRequirement
{
    private CtrlBDFunctionalRequirement ctrlBDFunctionalRequirement;
    private CtrlBDProject ctrlBDProject;
    private CtrlBDHumanResource ctrlBDHumanResource;
    private CtrlBDPriority ctrlBDPriority;
    private CtrlBDState ctrlBDState;
    private CtrlBDConflict ctrlBDConflict;
    private int _iIdProject;
    private int _iIdFunctionalRequirement;
    private List<itemConflict> lItemConflict;

    private struct itemConflict
    {
        public static int INSERTING = 1;
        public static int NEUTRAL = 0;
        public static int DELETING = -1;
        public static int MODIFYING = 2;

        public static int EXIST = 1;
        public static int DONTEXIST = 0;

        public int local;
        public int server;
        public Conflicts conflict;

        public itemConflict(int iLocal, int iServer)
        {
            local = iLocal;
            server = iServer;
            conflict = new Conflicts();
        }
    }

    
    public int iIdProject
    {
        get
        {
            return _iIdProject;
        }
        set
        {
            _iIdProject = value;
        }
    }

    public int iIdFunctionalRequirement
    {
        get
        {
            return _iIdFunctionalRequirement;
        }
        set
        {
            _iIdFunctionalRequirement = value;
        }
    }

    public CtrlFunctionalRequirement()
    {
        ctrlBDFunctionalRequirement = new CtrlBDFunctionalRequirement();
        ctrlBDProject = new CtrlBDProject();
        ctrlBDPriority = new CtrlBDPriority();
        ctrlBDState = new CtrlBDState();
        ctrlBDConflict = new CtrlBDConflict();
        lItemConflict = new List<itemConflict>();
        ctrlBDHumanResource = new CtrlBDHumanResource();


    }

    public void addFunctionalRequirement(String sName, String sDescription, String sJustification, int iSource, String sAcceptanceCriteria, int iPriority, int iState, DateTime dStartingDate, DateTime dConclucionDate, float fEffort, int iIdFather, bool bReusable, int iIdProject)
    {
        FunctionalRequirement functionalRequirement = new FunctionalRequirement(sName, sDescription, sJustification, iSource, sAcceptanceCriteria, iPriority, iState, dStartingDate, dConclucionDate, fEffort, iIdFather, bReusable, iIdProject);
        ctrlBDFunctionalRequirement.addFunctionalRequirement(functionalRequirement);
    }

    public void deleteFunctionalRequirement(int iId)
    {
        ctrlBDFunctionalRequirement.deleteFunctionalRequirement(iId);
    }

    public FunctionalRequirement getFunctionalRequirement(int iId)
    {
        FunctionalRequirement functionalRequirement = ctrlBDFunctionalRequirement.getFunctionalRequirement(iId);
        iIdFunctionalRequirement = functionalRequirement.iId;
        iIdProject = functionalRequirement.iIdProject;
        List<Conflicts> temp = ctrlBDConflict.getConflicts(iIdFunctionalRequirement);

        foreach(Conflicts i in temp)
        {
            functionalRequirement.lConflicts.Add(i);
        }
        return functionalRequirement;
    }

    public void setFunctionalRequirement(String sName, String sDescription, String sJustification, int iSource, String sAcceptanceCriteria, int iPriority, int iState, DateTime dStartingDate, DateTime dConclucionDate, float fEffort, int iIdFather, bool bReusable, int iIdProject)
    {
        FunctionalRequirement functionalRequirement = new FunctionalRequirement(iIdFunctionalRequirement, sName, sDescription, sJustification, iSource, sAcceptanceCriteria, iPriority, iState, dStartingDate, dConclucionDate, fEffort, iIdFather, bReusable, iIdProject);
        ctrlBDFunctionalRequirement.setFunctionalRequirement(functionalRequirement);
    }

    public DataSet getData()
    {
        return ctrlBDFunctionalRequirement.getData();
    }

    public DataSet getFilterData(int iIdProject, int iIdFather)
    {
        return ctrlBDFunctionalRequirement.getFilterData(iIdProject, iIdFather);
    }

    public void addConflict(int iIdConflicted, String sReason)
    {
        itemConflict temp = new itemConflict();
        temp.conflict.iIdConflicted = iIdConflicted;
        temp.conflict.sReason = sReason;
        temp.local = itemConflict.INSERTING;
        temp.server = itemConflict.DONTEXIST;

        bool esta = false;

        for (int i = 0; i < lItemConflict.Count && !(esta); ++i)
        {
            if(lItemConflict[i].conflict.iIdConflicted == iIdConflicted)
            {
                esta = true;
                temp.local = itemConflict.MODIFYING;
                temp.server = lItemConflict[i].server;
                lItemConflict.RemoveAt(i);
            }
        }

        lItemConflict.Add(temp);
    }

    public void deleteConflict(int iIdConflicted)
    {
        itemConflict temp = new itemConflict();
        temp.conflict.iIdConflicted = iIdConflicted;
        temp.local = itemConflict.DELETING;
        temp.server = itemConflict.EXIST;

        for (int i = 0; i < lItemConflict.Count; ++i)
        {
            if (lItemConflict[i].conflict.iIdConflicted == iIdConflicted)
            {
                lItemConflict.RemoveAt(i);

                if (lItemConflict[i].server == itemConflict.EXIST)
                {
                    lItemConflict.Add(temp);
                }
            }
        }
    }

    public DataSet getProjectesData()
    {
        return ctrlBDProject.getData();
    }

    public DataSet getSourceData()
    {
        return ctrlBDHumanResource.getData();
    }

    public DataSet getPriorityData()
    {
        return ctrlBDPriority.getData();
    }

    public DataSet getStateData()
    {
        return ctrlBDState.getData();
    }

}
